package com.smartHire.controller.User.Cart;

import com.alibaba.fastjson.JSONObject;
import com.smartHire.domain.Cart;
import com.smartHire.domain.Goods;
import com.smartHire.domain.Shop;
import com.smartHire.service.CartService;
import com.smartHire.service.GoodsService;
import com.smartHire.service.ShopService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet(name = "ShowCartServlet", value = "/ShowCartServlet")
public class ShowCartServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String open_id= request.getParameter("open_id");

        WebApplicationContext ctx = null;
        //使用框架中的方法，获取容器对象
        ServletContext sc = getServletContext();
        ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
        System.out.println("容器对象的信息========"+ctx);

        try {
            CartService cartService = (CartService) ctx.getBean("cartService");
            GoodsService goodsService = (GoodsService) ctx.getBean("goodsService");
            ShopService shopService = (ShopService) ctx.getBean("shopService");
            Cart cart = new Cart();
            cart.setU_Id(open_id);
            List<Cart> carts = cartService.queryCartByU_id(cart);

            response.setCharacterEncoding("utf-8");


            List list = new ArrayList();
            for(Cart cart1:carts)
            {
                int goodsId = cart1.getGoods();
                Goods goods = new Goods();
                goods.setGood_id(goodsId);
                goods = goodsService.queryGoodsByID(goods);

                Shop shop = new Shop();
                shop.setShop_id(goods.getShop_id());
                shop = shopService.queryShopById(shop);

                list.add(cart1);
                list.add(goods);
                list.add(shop);




            }

            String jsonStr = JSONObject.toJSONString(list);
            response.getWriter().write(jsonStr);

        }catch (Exception e)
        {
            e.printStackTrace();
        }



    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
